﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ewal.Msg;

namespace nbb.darbo_grafikas
{
    public partial class FormChangeDefectoscopes : Form
    {
        List<Defektoskopas> visiDefektoskopai = new List<Defektoskopas>();
        List<int> dfIdsBeforeEditing;
        List<int> dfIdsToRemove;

        public FormChangeDefectoscopes()
        {
            InitializeComponent();
        }
        
        private void FormChangeDefectoscopes_Load(object sender, EventArgs e)
        {
            dfIdsBeforeEditing = new List<int>();
            dfIdsToRemove = new List<int>();
            foreach (Defektoskopas df in Program.Defektoskopai) dfIdsBeforeEditing.Add(df.Id);
            
            DbCommunicator.fetchDefektoskopai(visiDefektoskopai);

            for (int i = 0; i < visiDefektoskopai.Count; i++ )
            {
                dgv.Columns.Add(new DataGridViewCheckBoxColumn());
                dgv.Columns[i].Name = visiDefektoskopai[i].Id.ToString();
                dgv.Columns[i].HeaderText = visiDefektoskopai[i].Kodas;
            }

            dgv.Rows.Add();

            DefektoskopasComparer dfcomp = new DefektoskopasComparer();
            for (int i = 0; i < visiDefektoskopai.Count; i++)
            {
                (dgv[i, 0]).Value = Program.Defektoskopai.Contains<Defektoskopas>(visiDefektoskopai[i], dfcomp);                   
            }
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            // Reikia į Program.Defektoskopai patalpinti tuos defektoskopus,
            // kurie pažymėti paukščiuku ir ta tvarka, kaip jie yra išdėstyti datagridview.

            // key: Column.DisplayIndex, value: Defektoskopas.Id
            Dictionary<int, int> displId_dsId = new Dictionary<int, int>();
            foreach (DataGridViewCell cell in dgv.Rows[0].Cells)
            {
                if ((bool)cell.Value)
                {
                    displId_dsId.Add(cell.OwningColumn.DisplayIndex, Int32.Parse(cell.OwningColumn.Name));
                }
            }

            // atsiklausiama, ar galima trinti darbus;
            foreach (int id in dfIdsBeforeEditing) // visi prieš tai buvę defektoskopai
            {
                if (!displId_dsId.ContainsValue(id) && // nebepatenka į naują sąrašą
                    Program.Darbai.Exists(darbas => darbas.Defektoskopas.Id == id)) // bet darbai jiems egzistuoja
                {
                    string kodas = visiDefektoskopai.Find(df => df.Id == id).Kodas;
                    if (Msg.OkCancelQuestion(string.Format(Messages.Default.JobsGonnaBeDeleted, kodas)) == DialogResult.Cancel)
                        return;
                    else dfIdsToRemove.Add(id);
                }
            }

            foreach (int id in dfIdsToRemove)
            {
                Program.Darbai.RemoveAll(darbas => darbas.Defektoskopas.Id == id);
            }

            Program.Defektoskopai.Clear();
            for (int i = 0; i < visiDefektoskopai.Count; i++)
            {
                if (displId_dsId.ContainsKey(i))
                    Program.Defektoskopai.Add(visiDefektoskopai.Find(def => def.Id == displId_dsId[i]));
            }

        }

    }
}
